import Dexie from 'dexie'

export class MyDB extends Dexie {
  allText: Dexie.Table<Text, number>
  allIMG: Dexie.Table<Image, number>
  XMLGen: Dexie.Table<XMLGen, string>
  tempOrder: Dexie.Table<Order, string>
  template: Dexie.Table<Template, string>

  constructor (name: string = 'EPUB_DB') {

    super(name)

    this.version(1).stores({
      allText: '++id, source, title, path',
      allIMG: '++id, source, name, mime, ext',
      tempOrder: 'name, source',
      template: 'name, path, source',
      XMLGen: 'name, source'
    })
  }

  async renew () {
    return Promise.all([this.allText.clear(), this.allIMG.clear(), this.XMLGen.clear(), this.template.clear(), this.XMLGen.clear()])
  }
}

export interface Text {
  id?: number
  source: string
  title: string
  path: string
}

export interface Image {
  id?: number
  source: Blob
  name: string
  mime: string
  ext: string
}

export interface Template {
  name: string
  path: string
  source: string
}

interface XMLType {
  opfSpine: string,
  opfManifest: string,
  ncx: string
}
export interface XMLGen {
  name: keyof XMLType
  source: string
}
type OrderType = 'play' | 'sec' | 'color'
export interface Order {
  name: OrderType
  source: number
}

export let DB = new MyDB()
